package es.puravida.watchuoc.client;

import es.puravida.watchuoc.R;
import es.puravida.watchuoc.WatchUOCApplication;
import es.puravida.watchuoc.R.id;
import es.puravida.watchuoc.R.layout;
import es.puravida.watchuoc.service.WatchUOCService;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class WatchUOCActivity extends Activity {

	private static final String TAG = WatchUOCActivity.class.getName();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Log.d(TAG, "starting activity");

		TextView username = (TextView) this.findViewById(R.id.edit_name);
		TextView pwd = (TextView) this.findViewById(R.id.edit_pwd);
		TextView min = (TextView) this.findViewById(R.id.edit_ruido);

		SharedPreferences settings = getSharedPreferences("watchuoc",
				MODE_PRIVATE);
		username.setText(settings
				.getString("es.puravida.watchuoc.username", ""));
		Integer tmp = settings.getInt("es.puravida.watchuoc.minmensajes", 1);
		min.setText(tmp.toString());

		if (username.getText().length() > 0) {
			pwd.requestFocus();
		}

		Button b = (Button) this.findViewById(R.id.buttonVigilar);
		Log.i(TAG, "button " + b);
		b.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Log.d(TAG, "start WatchUOCService");
				startWatchUOCService();
				endActivity();
			}
		});

		b = (Button) this.findViewById(R.id.buttonGuardar);
		b.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				new Thread(new Runnable() {
					public void run() {
						savePreferences();
					}
				}).start();
			}
		});

		Spinner spinner = (Spinner) this.findViewById(R.id.spinnerRefresco);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.refresco_array,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		Integer pos = settings.getInt("es.puravida.watchuoc.interval", 0);
		spinner.setSelection(pos);
	}

	protected void endActivity() {
		this.finish();
	}

	protected void startWatchUOCService() {
		Intent launch = new Intent(WatchUOCActivity.this, WatchUOCService.class);
		TextView username = (TextView) this.findViewById(R.id.edit_name);
		TextView pwd = (TextView) this.findViewById(R.id.edit_pwd);
		TextView min = (TextView) this.findViewById(R.id.edit_ruido);

		launch.putExtra("es.puravida.watchuoc.username", username.getText()
				.toString());
		launch.putExtra("es.puravida.watchuoc.password", pwd.getText()
				.toString());
		launch.putExtra("es.puravida.watchuoc.minmensajes",
				Integer.parseInt(min.getText().toString()));
		launch.putExtra("es.puravida.watchuoc.interval", getIntervalSelected());
				
		startService(launch);
	}

	protected void savePreferences() {
		String username = ((TextView) this.findViewById(R.id.edit_name))
				.getText().toString();
		String min = ((TextView) this.findViewById(R.id.edit_ruido)).getText()
				.toString();
		Spinner spinner = (Spinner) this.findViewById(R.id.spinnerRefresco);
		int pos = spinner.getSelectedItemPosition();
		SharedPreferences settings = getSharedPreferences("watchuoc",
				MODE_PRIVATE);
		Editor editor = settings.edit();
		editor.putString("es.puravida.watchuoc.username", username);
		editor.putInt("es.puravida.watchuoc.minmensajes", Integer.parseInt(min));
		editor.putInt("es.puravida.watchuoc.interval", pos);
		editor.commit();
		this.runOnUiThread(new Runnable() {
			public void run() {
				Toast.makeText(WatchUOCActivity.this, "Preferencias guardadas",
						Toast.LENGTH_LONG).show();
			}
		});
	}

	protected int getIntervalSelected() {
		int ret = 15;
		Spinner spinner = (Spinner) this.findViewById(R.id.spinnerRefresco);
		int pos = spinner.getSelectedItemPosition();
		String str = spinner.getItemAtPosition(pos).toString();
		ret = Integer.parseInt(str.split(" ")[0]);
		return ret;
	}
}